<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <title>dc-example</title>
    <script src="/libs/dc-sdk/dc.min.js"></script>
    <link href="/libs/dc-sdk/dc.min.css" type="text/css" rel="stylesheet" />
    <link href="../index.css" type="text/css" rel="stylesheet" />
  </head>

  <body>
    <div id="viewer-container" class="viewer-container"></div>
    <div class="btn-box">
      <ul>
        <li><input type="checkbox" name="check_loop" /> 是否循环</li>
        <li><button onclick="start()">开始</button></li>
        <li><button onclick="pause()">暂停</button></li>
        <li><button onclick="restore()">继续</button></li>
      </ul>
    </div>
    <script>
      DC.config.baseUrl = '../libs/dc-sdk/resources/'
      let viewer = new DC.Viewer('viewer-container')
      let baseLayer = DC.ImageryLayerFactory.createAMapImageryLayer({
        style: 'img',
        crs: 'WGS84',
      })
      viewer.addBaseLayer(baseLayer, { brightness: 0.1 })
      let layer = new DC.TilesetLayer('layer').addTo(viewer)
      let tileset = new DC.Tileset(
        '//resource.dvgis.cn/data/3dtiles/ljz/tileset.json'
      )
      let customShader = new DC.CustomShader({
        fragmentShaderText: `
      void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) {
         vec4 position = czm_inverseModelView * vec4(fsInput.attributes.positionEC,1); // 位置
         float glowRange = 100.0; // 光环的移动范围(高度)
         vec4 temp = vec4(0.2,  0.5, 1.0, 1.0); // 颜色
         temp *= vec4(vec3(position.z / 100.0), 1.0);  // 渐变
         // 动态光环
         float time = fract(czm_frameNumber / 360.0);
         time = abs(time - 0.5) * 2.0;
         float diff = step(0.005, abs( clamp(position.z / glowRange, 0.0, 1.0) - time));
         material.diffuse = vec3(temp.rgb + temp.rgb * (1.0 - diff)) ;
       }
      `,
      })
      tileset.setCustomShader(customShader)
      layer.addOverlay(tileset)

      viewer.flyTo(tileset)

      let flying = undefined

      function start() {
        let checked = document.getElementsByName('check_loop')[0].checked
        flying = new DC.Flying(viewer, {
          loop: checked,
          dwellTime: 3,
        })
        flying.positions = [
          {
            lng: 121.46748793889597,
            lat: 31.22345700031846,
            alt: 1082.6691622203975,
            heading: 0.9161118327237789,
            pitch: -38.63414039808751,
          },
          {
            lng: 121.49543157056694,
            lat: 31.219611353179484,
            alt: 663.5376240776116,
            heading: 0.9161124649627334,
            pitch: -38.63418986635751,
          },
          {
            lng: 121.53162234574106,
            lat: 31.228003869427294,
            alt: 1891.926162456467,
            heading: 298.6565902579582,
            pitch: -33.67285705092492,
          },
          {
            lng: 121.54438164431083,
            lat: 31.25201585389836,
            alt: 1441.4625182144541,
            heading: 298.65660919687264,
            pitch: -33.6728415156399,
          },
        ]
        flying.start()
      }

      function pause() {
        flying.pause()
      }

      function restore() {
        flying.restore()
      }
    </script>
  </body>
</html>
